/*
 * @Author:  dongdong.wu
 * @Date: 2021-03-15 13:57:37
 * @Description: table 组件的table-column
 */

var dayjs = require("dayjs");
var currency = require("currency.js");

// 格式化
const cellValueFormat = (
  value,
  fName,
  format = "YYYY-MM-DD",
  precision = 2
) => {
  if (fName === "date") {
    // 日期格式化
    return dayjs(value).format(format);
  } else if (fName === "gender") {
    return value === "1" ? "女" : "男";
  } else if (fName === "flag") {
    return value ? "是" : "否";
  } else if (fName === "enable") {
    return value === false ? "禁用" : "启用";
  } else if (fName === "releaseAuction") {
    return value === false ? "未上架" : "已上架";
  } else if (fName === "syncState") {
    return value === true ? "已同步" : "未同步";
  } else if (fName === "result") {
    return value === "F" || value === false ? "失败" : "成功";
  } else if (fName === "sort") {
    return value === 0 ? "否" : "是";
  } else if (fName === "currency") {
    // 金额格式化, 默认两位小数
    return currency(value, {
      precision: precision,
      symbol: "¥"
    }).format();
  } else if (fName === "auditStatus") {
    // 格式化审核状态
    if (value === "notAdd") {
      return "未提交";
    } else if (value === "waitAudit") {
      return "待审核";
    } else if (value === "auditing") {
      return "审核中";
    } else if (value === "auditPass") {
      return "审核通过";
    } else if (value === "notPass") {
      return "已驳回";
    }
  } else if (fName === "mergeState") {
    // 格式化合并状态
    if (value === "merge") {
      return "已合并";
    } else {
      return "未合并";
    }
  } else if (fName === "productType") {
    // 格式化商品类型
    if (value === "common") {
      return "普通品";
    } else if (value === "dangerous") {
      return "危险品";
    } else if (value === "explosive") {
      return "易爆品";
    } else if (value === "drugs") {
      return "易制毒";
    }
  } else if (fName === "marginStatus") {
    // 格式化缴纳状态
    if (!value) {
      return "未缴纳";
    } else if (value === "paid") {
      return "已缴纳";
    } else if (value === "feedback") {
      return "已退回";
    } else if (value === "notpaid") {
      return "未缴纳";
    } else if (value === "transdeposit") {
      return "转定金";
    }
  } else if (fName === "blackLogType") {
    // 格式化黑名单记录操作状态
    if (value === "0") {
      return "移入";
    } else if (value === "1") {
      return "移除";
    }
  } else if (fName === "platform") {
    // 格式化平台类型
    if (value === "operator") {
      return "运营门户";
    } else if (value === "buyer") {
      return "采购商中心";
    } else if (value === "seller") {
      return "供应商中心";
    } else if (value === "carrier") {
      return "承运商";
    } else if (value === "warehouse") {
      return "仓储商";
    } else if (value === "financialServices") {
      return "金融服务商";
    } else if (value === "supplier") {
      return "平台供应商";
    }
  } else if (fName === "menuType") {
    // 格式化菜单类型
    if (value === "catalog") {
      return "目录";
    } else if (value === "menu") {
      return "菜单";
    } else if (value === "btn") {
      return "按钮";
    }
  } else if (fName === "biddingMode") {
    // 格式化竞价模式
    // if (value == "open_outcry") {
    //   return "公开竞价"
    // } else
    if (value === "free_bidding") {
      return "自由竞价";
    } else if (value === "component_bidding") {
      return "分量竞价";
    }
    // else if (value == "follow_price") {
    //   return "分量跟价"
    // }
  } else if (fName === "releaseStatus") {
    // 格式化发布状态
    if (value === "no") {
      return "未发布";
    } else if (value === "already") {
      return "已发布";
    } else if (value === "offShelf") {
      return "已下架";
    } else if (value === "disabled") {
      return "已失效";
    }
  }
  else if(fName === 'goodsTotal'){
    if(value == undefined || value == null){
      return 0;
    }
    return value.toFixed(4);
  } else if(fName === 'orderAmount'){
    if(value == undefined || value == null || value == 0){
      return 0;
    }
    const num = parseFloat(value);
    return num.toFixed(4);
  }else if(fName === 'count'){
    if(value == undefined || value == null || value == 0){
      return 0;
    }
    const num = parseFloat(value);
    return num.toFixed(2);
  }else if(fName === 'isAccPeriod'){
    if(value == "T"){
      return '是';
    }else {
      return '否'
    }
  }
  else {
    return value;
  }
};
const centerByFormat = function(item) {
  return (
    item.fName === "date" ||
    item.fName === "gender" ||
    item.fName === "boolean" ||
    item.fName === "enable" ||
    item.fName === "releaseStatus" ||
    item.fName === "sort" ||
    item.fName === "center" ||
    item.fName === "marginStatus"
  );
};
const rightByFormat = function(item) {
  return (
    item.fName === "orderAmount"||
    item.fName === "currency"
  );
};
// ele-tablue-column options
const columnOptions = function(h, item) {
  return {
    props: {
      minWidth: item.minWidth || 100,
      width: item.width,
      prop: item.prop,
      label: item.label,
      showOverflowTooltip: true,
      sortable: item.sortable,
      fixed: item.fixed || false,
      headerAlign:
        item.fName === "currency"
          ? "right"
          : centerByFormat(item)
          ? "center"
          : item.headerAlign || "center",
      align:
        rightByFormat(item)
          ? "right"
          : centerByFormat(item)
          ? "center"
          : item.align || "left"
    },
    key: item.prop,
    scopedSlots: {
      // 表体以及留出自定义表体插槽, 自定义表体模板，并继续跑出slot。供开发人员自定义
      default: props => {
        const bodySlot = this.$scopedSlots[item.prop];
        if (bodySlot) {
          return h(
            "span",
            {
              class: {
                "ele-table-column--slot": true
              },
              attrs: {
                id: `${props.$index}-${item.prop}`,
                "data-index": props.$index,
                "data-slot": `${item.prop}Rule`
              }
            },
            bodySlot({
              row: props.row,
              index: props.$index
            })
          );
        } else {
          // 没有自定义模板直接添加数值
          return h(
            "span",
            cellValueFormat(
              props.row[item.prop],
              item.fName,
              item.format,
              item.precision
            )
          );
        }
      },
      //表头以及自定义表头插槽. 自定义表头模板，并继续抛出slot。供开发人员自定义
      header: () => {
        // el-table-column自定义模板
        const headerSlot = this.$slots[`header-${item.prop}`];
        if (headerSlot) {
          return h("span", headerSlot);
        } else {
          return h("span", item.label);
        }
      }
    }
  };
};
// 渲染多级表头
const renderColumn = function(h, headers) {
  if (headers.child && headers.child.length > 0) {
    return headers.child.map(item => {
      return h("el-table-column", columnOptions.call(this, h, item), [
        renderColumn.call(this, h, item)
      ]);
    });
  }
};
export { columnOptions, renderColumn };
